package com.ymx.darling.type;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.Objects;

public class LocalDateTimeHandler implements TypeHandler<LocalDateTime> {
    @Override
    public LocalDateTime getResult(ResultSet resultSet, int columIndex) throws SQLException {
        Timestamp timestamp = resultSet.getTimestamp(columIndex);
        return Objects.isNull(timestamp) ? null : timestamp.toLocalDateTime();
    }

    @Override
    public void setParameter(PreparedStatement statement, LocalDateTime value, int index) throws SQLException {
        statement.setTimestamp(index, Timestamp.valueOf((value)));
    }
}
